<template>
  <kr-print-designer
    ref="printDesigner"
    :temp-value="value"
    :widget-options="widgets"
    @save="handleSave"
  />
</template>
<script>
import { OutStockOptions, InStockOptions } from "../data/options";
import { getPrintFields, getPrintTpl } from "./print.api";
export default {
  data() {
    return {
      index: "",
      value: {
        title: "demo",
        width: 750,
        height: 550,
        pageWidth: 750,
        pageHeight: 550,
        tempItems: [],
      },
      //widgets: OutStockOptions,
      widgets: [],
    };
  },

  created() {
    this.index = this.$route.query.index;
    try {
      let tempList = JSON.parse(localStorage.getItem("tempList")) || [];
      let getTplParams = {
        businessCode: "purchaseprebill",
        type: "1",
      };
      getPrintTpl(getTplParams).then((resp) => {
        this.value = resp.data.result;
        console.log("this.value:" + JSON.stringify(this.value));
      });
      this.value = tempList[this.index];
      getPrintFields().then((resp) => {
        console.log("result:", resp.data.result);
        this.widgets = resp.data.result;
        console.log("widgets:", this.widgets);
        // console.log(OutStockOptions);
      });
      // this.widgets = this.value.type == 1 ? OutStockOptions : InStockOptions;
      this.widgets = OutStockOptions;
    } catch (err) {
      console.error(err);
    }
  },
  methods: {
    handleSave(data) {
      let tempList = JSON.parse(localStorage.getItem("tempList")) || [];
      tempList[this.index] = data;
      localStorage.setItem("tempList", JSON.stringify(tempList));
      this.$router.back();
    },
    // 手动初始话模板数据
    initTemp() {
      this.$refs.printDesigner.initTemp(this.value, this.widgets);
    },
  },
};
</script>
